package Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Iterator;

@Controller
//@RequestMapping()
public class MainController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
//    @ResponseBody
    public String welcome(){
        return "index";
    }

    @RequestMapping(value = "/register", method = RequestMethod.GET)
//    @ResponseBody
    public String register(){
        return "register";
    }
    @RequestMapping(value = "/login", method = RequestMethod.GET)
//    @ResponseBody
    public String login(){
        return "login";
    }
    @RequestMapping(value = "/main", method = RequestMethod.GET)
//    @ResponseBody
    public String main(){
        return "main";
    }

    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
//    @ResponseBody
    public String uploadImg(HttpServletRequest request) throws IOException {
        long  startTime=System.currentTimeMillis();
        //将当前上下文初始化给  CommonsMutipartResolver （多部分解析器）
        CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(
                request.getSession().getServletContext());
        //检查form中是否有enctype="multipart/form-data"
        if(multipartResolver.isMultipart(request))
        {
            //将request变成多部分request
            MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
            //获取multiRequest 中所有的文件名
            Iterator iter=multiRequest.getFileNames();

            while(iter.hasNext())
            {
                //一次遍历所有文件
                MultipartFile file=multiRequest.getFile(iter.next().toString());
                if(file!=null)
                {
                    String path="E:/springUpload"+file.getOriginalFilename();
                    //上传
                    file.transferTo(new File(path));
                }
            }

        }
        long  endTime=System.currentTimeMillis();
        System.out.println("方法三的运行时间："+String.valueOf(endTime-startTime)+"ms");
        return "main";
    }
}